package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.ICompanyDao;
import com.cn.wanxi.dao.impl.CompanyDaoImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements ICompanyService {
    ICompanyDao companyDao = new CompanyDaoImpl();

    @Override
    public CompanyModel getCompany() {
        Jedis jedis = new Jedis();
        CompanyModel companyModel = new CompanyModel();
        try {
            if (jedis.get("company_id") != null)
                companyModel = companyDao.getCompanyByRedis();
            else {
                companyModel = companyDao.getCompany();
                companyDao.setCompanyInRedis(companyModel);
            }
        } catch (Exception e) {//抓住异常，如果Redis缓存服务未打开，则也应及时加载数据库数据
            companyModel = companyDao.getCompany();
        }
        return companyModel;
    }

    @Override
    public int editCompany(CompanyModel company) {
        return companyDao.editCompany(company);
    }
}
